package com.zhl.media.app.mvp.ui.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhl.media.app.R;
import com.zhl.media.app.di.component.DaggerWorkProductComponent;
import com.zhl.media.app.di.module.WorkProductModule;
import com.zhl.media.app.greendao.MediaPhotoDao;
import com.zhl.media.app.mvp.adapter.WorkProdListViewAdapter;
import com.zhl.media.app.mvp.contract.WorkProductContract;
import com.zhl.media.app.mvp.presenter.WorkProductPresenter;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonres.base.AppActivity;
import com.zhl.media.commonres.utils.DatePickUtils;
import com.zhl.media.commonres.view.PublicTitleBar;
import com.zhl.media.commonsdk.core.EventBusCode;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.commonsdk.entity.MyPushProjectApp;
import com.zhl.media.commonsdk.entity.MyPushResultApp;
import com.zhl.media.commonsdk.utils.Utils;

import org.simple.eventbus.Subscriber;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

@Route(path = RouterHub.APP_WORKPRODACTIVITY)
public class WorkProductActivity extends AppActivity<WorkProductPresenter> implements WorkProductContract.View {

    @BindView(R.id.titleBar)
    PublicTitleBar titleBar;
    @BindView(R.id.list_view)
    ListView listView;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    TextView zsTv;

    TextView yscTv;

    private String date;
    private List<MyPushProjectApp> mData;
    private MediaPhotoDao mediaPhotoDao;
    private WorkProdListViewAdapter workProdListViewAdapter;
    private TextView weishangchuan;
    private TextView dscNum;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerWorkProductComponent.builder().appComponent(appComponent).workProductModule(new WorkProductModule(this)).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_work_product; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    // 更新列表结果
    @Subscriber(tag = "update_skjg")
    public void updateSkjg(int code) {
        if(code == EventBusCode.SKJG){
            refreshLayout.autoRefresh();
        }
    }

    // 更新待上传数
    @Subscriber(tag = "update_dsc_num")
    public void update_dsc_num(int code) {
        if(code == EventBusCode.DSCNUM){
            loadTags();
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        titleBar.setTitle("上刊结果");
        mediaPhotoDao = EntityManager.getInstance(this).getMediaPhotoDao();
        date = Hawk.get("thisDate","0");
        View headerView = LayoutInflater.from(this).inflate(R.layout.work_prod_list_header,null);
        listView.addHeaderView(headerView);
        initHeader(headerView);
        mData = new ArrayList<MyPushProjectApp>();
        workProdListViewAdapter = new WorkProdListViewAdapter(this,R.layout.wrok_prod_item,mData);
        listView.setAdapter(workProdListViewAdapter);
        loadTags();
    }

    private void loadTags() {
        int size = EntityManager.getInstance(this).getMediaPhotoDao().loadAll().size();
        if(size>0){
            dscNum.setVisibility(View.VISIBLE);
        }else {
            dscNum.setVisibility(View.GONE);
        }
    }
    private void initHeader(View headerView) {
        TextView workDate = (TextView) headerView.findViewById(R.id.workDate);
        weishangchuan = (TextView) headerView.findViewById(R.id.weishangchuan);
        LinearLayout sclist = (LinearLayout) headerView.findViewById(R.id.sclist);
        zsTv = (TextView) headerView.findViewById(R.id.zs);
        dscNum = (TextView) headerView.findViewById(R.id.dsc_num);
        dscNum.setWidth(15);
        dscNum.setHeight(15);
        yscTv = (TextView) headerView.findViewById(R.id.ysc);
        workDate.setText(date);
        SimpleDateFormat sdf = new SimpleDateFormat("y-MM-dd");
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        workDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(WorkProductActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        calendar.set(Calendar.YEAR,year);
                        calendar.set(Calendar.MONTH,month);
                        calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                        String time = sdf.format(calendar.getTime());
                        workDate.setText(time);
                        date = time;
                        refreshLayout.autoRefresh();
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });
        sclist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(RouterHub.APP_UPLOADACTIVITY).withString("date",date).navigation(WorkProductActivity.this);
            }
        });
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.pushResult(date);
            }
        });
    }
    @Override
    public void showLoading() {

    }
    @Override
    public void hideLoading() {
        refreshLayout.finishRefresh();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }
    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }
    @Override
    public void killMyself() {
        finish();
    }
    @Override
    public void onSuccess(MyPushResultApp data) {
        zsTv.setText(data.getCurrentTotalNum() + "");
        yscTv.setText(data.getYscNum() + "");
        weishangchuan.setText(data.getWscNum() + "");
        mData.clear();
        mData.addAll(data.getMyProjectInfo());
        workProdListViewAdapter.notifyDataSetChanged();
    }
}
